package com.example.provider.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.example.common.base.BaseController;
import com.example.common.base.Paging;
import com.example.common.base.Result;
import com.example.common.model.ActivityArticle;
import com.example.common.model.ArticleArticleParticipants;
import com.example.common.model.User;
import com.example.common.model.condition.ActivityArticleCondition;
import com.example.provider.service.ActivityArticleService;
import com.example.provider.service.Impl.ActivityArticleServiceImpl;
import com.example.provider.service.Impl.ArticleArticleParticipantsServiceImpl;
import com.example.provider.service.Impl.UserAttentionServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;



/**
 * 活动文章表Controller
 * 
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Api(tags = "活动文章表服务")
@RestController
@RequestMapping("/activityArticle")
//@CrossOrigin(origins = "http://localhost:8080")
public class ActivityArticleController extends BaseController {
    @Autowired
    private ActivityArticleService activityArticleService;

    @Autowired
    private ActivityArticleServiceImpl activityArticleServiceImpl;

    @Autowired
    private ArticleArticleParticipantsServiceImpl articleArticleParticipantsService;

    @Autowired
    private UserAttentionServiceImpl userAttentionService;

    @RequestMapping("/test")
    public String test(){
        return "is ok!";
    }

    @ApiOperation(value = "根据查询参数分页查询活动文章表列表")
    @ApiImplicitParam(name = "condition", value = "活动文章表查询条件", required = true, dataType = "ActivityArticleCondition", paramType = "body")
    @PostMapping("/list")
    public Paging<ActivityArticle> list(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("type") Integer activityType) {
        ActivityArticleCondition condition = new ActivityArticleCondition();
        condition.setPage(pageNum.longValue());
        condition.setPageSize(pageSize.longValue());
        IPage<ActivityArticle> page = activityArticleService.findActivityArticleByCondition(condition, activityType);
        return Paging.buildPaging(page);
    }

    /**
     *  查询详细界面 用户是否关注，活动参与的用户，以及自己是否参与活动
     */
    @ApiOperation(value = "根据查询参数查询活动文章详情")
    @ApiImplicitParam(name = "")
    @GetMapping("/allActivityTypeList/{type}")
    public Map<String, Object> listType(@PathVariable Integer type){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("activity", activityArticleServiceImpl.findActivityAllTypeList(type));
        map.put("success", 200);
        return map;
    }


    /**
     *
     */
    @ApiOperation(value = "根据主键ID查询活动文章表相关的关联信息")
    @ApiImplicitParam(name = "aId", value = "主键ID", required = true)
    @GetMapping(value = "/get")
    public Map<String, Object> get(
            @RequestParam("aId") Integer aId,
            @RequestParam("userId") String userId,
            @RequestParam("authorId") String authorId) throws ExecutionException, InterruptedException {
        Map<String, Object> map = new HashMap<>();
        if (aId == null) {
            map.put("error", "请选择需要查询的数据！");
            return map;
        }
        //异步调用
        map.put("detail", activityArticleService.getActivityArticleById(aId).get());
        map.put("participantsNum", articleArticleParticipantsService.findParticipants(aId).get());
        map.put("checkUserAttention", userAttentionService.checkUserAttention(userId, authorId));
        return map;
    }

    /**
     *  参与活动
     */
    @ApiOperation(value = "参与活动，增加活动参与表")
    @ApiImplicitParam(name = "activityArticle", value = "活动文章表", required = true, dataType = "ActivityArticle", paramType = "body")
    @PostMapping("/addArticleArticleParticipants")
    public Boolean addArticleArticleParticipants(
            @RequestParam("aId") Integer aId,
            @RequestParam("userId") String userId,
            @RequestParam("userName") String userName,
            @RequestParam("userAvatarUrl")String userAvatarUrl) {

        return articleArticleParticipantsService.addArticleArticleParticipants(aId, userId, userName, userAvatarUrl);

    }

    /**
     *  参与活动
     */
    @ApiOperation(value = "新增活动文章表信息")
    @ApiImplicitParam(name = "activityArticle", value = "活动文章表", required = true, dataType = "ActivityArticle", paramType = "body")
    @PostMapping("/add")
    public Result<?> add(@RequestBody ActivityArticle activityArticle) {
        boolean bool = activityArticleService.addActivityArticle(activityArticle);
        if (bool) {
            return Result.ok(activityArticle);
        }
        return Result.failed();
    }

    /**
     *  取消参与活动
     */

    @PostMapping("/reArticleArticleParticipants")
    public Boolean reArticleArticleParticipants(
            @RequestParam("aId") Integer aId,
            @RequestParam("userId") String userId){
        return articleArticleParticipantsService.reArticleArticleParticipants(aId, userId);
    }

    @ApiOperation(value = "修改活动文章表信息")
    @ApiImplicitParam(name = "activityArticle", value = "活动文章表", required = true, dataType = "ActivityArticle", paramType = "body")
    @PutMapping(value = "/update")
    public Result<?> update(@RequestBody ActivityArticle activityArticle) {
        Integer aId = activityArticle.getAId();
        if (aId == null) {
            return Result.failed("请选择需要修改的数据！");
        }
        boolean bool = activityArticleService.updateActivityArticle(activityArticle);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID删除活动文章表")
    @ApiImplicitParam(name = "aId", value = "主键ID", required = true)
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable Integer aId) {
        if (aId == null) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = activityArticleService.deleteActivityArticleById(aId);
        return Result.okOrFailed(bool);
    }

    @ApiOperation(value = "根据主键ID列表批量删除活动文章表")
    @ApiImplicitParam(name = "idList", value = "主键ID列表", required = true, allowMultiple = true, paramType = "body")
    @DeleteMapping("/deleteList")
    public Result<?> deleteList(@RequestBody List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Result.failed("请选择需要删除的数据！");
        }
        boolean bool = activityArticleService.deleteActivityArticleByIds(idList);
        return Result.okOrFailed(bool);
    }
}